package com.ljkj.blockpay.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;

import com.flyco.tablayout.CommonTabLayout;
import com.ljkj.blockpay.R;

/**
 * Created by yp on 2022/3/1
 */
public class CommonDialog extends Dialog {
    private AlertController alertController;


    public CommonDialog(Context context, int themeResId) {
        super(context, themeResId);
        alertController = new AlertController(this, getWindow());
    }


    public CommonDialog setText(@IdRes int viewId, CharSequence text) {
        alertController.setText(viewId, text);
        return this;
    }


    public CommonDialog setText(@IdRes int viewId, @StringRes int stringRes) {
        alertController.setText(viewId, getContext().getString(stringRes));
        return this;
    }


    public CommonDialog setOnclickListener(@IdRes int viewId, View.OnClickListener listener) {
        alertController.setOnclickListener(viewId, listener);
        return this;
    }

    public <T extends View> T getView(@IdRes int viewId) {
        return alertController.getView(viewId);
    }


    public CommonDialog setVisible(@IdRes int view, int visible) {
        alertController.setGone(view, visible);
        return this;
    }


    public static class Builder {
        private AlertController.AlertParams params;
        private Context context;

        public static Builder with(Context context, int themeRes) {
            return new Builder(context, themeRes);
        }


        public static Builder with(Context context) {
            return new Builder(context, R.style.dialog);
        }

        public Builder(Context context){
            this(context, R.style.dialog);
        }


        private Builder(Context context, int themeRes) {
            this.context = context;
            params = new AlertController.AlertParams(context, themeRes);
        }

        public Builder setContentView(View view) {
            params.mView = view;
            params.mViewLayoutResId = 0;
            return this;
        }

        public Builder setContentView(@LayoutRes int resid) {
            params.mViewLayoutResId = resid;
            params.mView = null;
            return this;
        }

        /**
         * 设置文本
         *
         * @param view
         * @param text
         * @return
         */
        public Builder setText(@IdRes int view, CharSequence text) {
            params.mTextArray.put(view, text);
            return this;
        }


        public Builder setText(@IdRes int view, @StringRes int stringRes) {
            params.mTextArray.put(view, context.getString(stringRes));
            return this;
        }

        /**
         * 设置点击事件
         *
         * @param view
         * @param listener
         * @return
         */
        public Builder setOnClickListener(@IdRes int view, View.OnClickListener listener) {
            params.mClickArray.put(view, listener);
            return this;
        }

        /**
         * 设置View显示隐藏
         *
         * @param view
         * @param visibility
         * @return
         */
        public Builder setVisibility(@IdRes int view, int visibility) {
            params.mVisibility.put(view, visibility);
            return this;
        }

        /**
         * 是否充满屏幕宽度
         *
         * @return
         */
        public Builder fullWidth() {
            params.mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
            return this;
        }

        /**
         * 是否充满屏幕高度
         *
         * @return
         */
        public Builder fullHeight() {
            params.mHeight = ViewGroup.LayoutParams.MATCH_PARENT;
            return this;
        }

        /**
         * 从底部弹出
         *
         * @param isAnimation 是否有动画
         * @return
         */
        public Builder formBottom(boolean isAnimation) {
            if (isAnimation) {
                params.mAnimations = R.style.dialog_from_bottom_anim;
            }
            params.mGravity = Gravity.BOTTOM;
            return this;
        }

        /**
         * 从底部弹出
         *
         * @param isAnimation 是否有动画
         * @return
         */
        public Builder formBottom2(boolean isAnimation) {
            if (isAnimation) {
                params.mAnimations = R.style.dialog_from_bottom_anim2;
            }
            params.mGravity = Gravity.BOTTOM;
            return this;
        }


        /**
         * 设置dialog在屏幕中显示的位置
         *
         * @param gravity
         * @return
         */
        public Builder forGravity(int gravity) {
            params.mGravity = gravity;
            return this;
        }

        /**
         * 设置Dialog的宽高
         *
         * @param width
         * @param height
         * @return
         */
        public Builder setWidthAndHeight(int width, int height) {
            params.mWidth = width;
            params.mHeight = height;
            return this;
        }

        /**
         * 添加默认动画
         *
         * @return
         */
        public Builder addDefaultAnimation() {
            params.mAnimations = R.style.dialog_scale_anim;
            return this;
        }

        /**
         * 设置动画
         *
         * @param styleAnimation
         * @return
         */
        public Builder setAnimations(int styleAnimation) {
            params.mAnimations = styleAnimation;
            return this;
        }

        /**
         * 点击空白是否能够取消  默认点击阴影不可以取消
         */
        public Builder setCancelable(boolean cancelable) {
            params.mCancelable = cancelable;
            return this;
        }


        /**
         * 点击返回键监听事件
         */
        public Builder setOnKeyListener(OnKeyListener onKeyListener) {
            params.mOnKeyListener = onKeyListener;
            return this;
        }

        /**
         * 取消监听事件
         */
        public Builder setOnCancelListener(OnCancelListener onCancelListener) {
            params.mOnCancelListener = onCancelListener;
            return this;
        }

        /**
         * 消失监听事件
         */
        public Builder setOnDismissListener(OnDismissListener onDismissListener) {
            params.mOnDismissListener = onDismissListener;
            return this;
        }

        /**
         * 创建dialog
         *
         * @return
         */
        public CommonDialog create() {
            CommonDialog dialog = new CommonDialog(params.mContext, params.mThemeResId);
            params.apply(dialog.alertController);
            dialog.setCancelable(params.mCancelable);
            dialog.setCanceledOnTouchOutside(params.mCancelable);
            dialog.setOnCancelListener(params.mOnCancelListener);
            dialog.setOnDismissListener(params.mOnDismissListener);
            if (params.mOnKeyListener != null) {
                dialog.setOnKeyListener(params.mOnKeyListener);
            }
            return dialog;
        }

        /**
         * 显示dialog
         *
         * @return
         */
        public CommonDialog show() {
            CommonDialog dialog = create();
            dialog.show();
            return dialog;
        }
        public void dismiss(){
            create().dismiss();
        }
    }

}
